package cn.controller;

import cn.basic.BaseController;
import cn.dao.ClassRoomDAO;
import cn.factory.DAOFactory;
import cn.model.TClassRoom;
import cn.util.Expression;
import cn.util.Toolsutil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 教室信息数据交互
 * @author WZG
 * @version 2020-2-30
 */
@Controller
@RequestMapping(value="/classroom")
public class ClassRoomController extends BaseController {


    @RequestMapping(value="/getroomlistByPage")//映射路径,拦截post请求
    public void getClassRoomList(
            String roomName,
            String roomtype,
            Integer state,
            Integer page,
            Integer limit,//一页多少条

            HttpServletRequest request,
            HttpServletResponse response,
            Model model) throws IOException {

        Expression exp = new Expression();
        if(roomName != null && !roomName.equals("")){
            exp.andEqu("roomName", roomName, String.class);}
        if (roomtype !=null && !roomtype.equals("")){
            exp.andEqu("roomtype",roomtype,String.class);
        }
        if (state!=null && !state.equals("")){
            exp.andEqu("state",state,Integer.class);
        }
        String wherecondition = exp.toString();

        ClassRoomDAO crdao = DAOFactory.getClassRomeDAO();
        List<TClassRoom> list = crdao.getClassListByPage(wherecondition,page, limit);
        int size = crdao.getClassAmount();
        if (list!=null) {
            dt.code = dt.FLAG_SUCC;
            dt.msg = "查询成功，共查出" + dt.count;
            dt.data = list;
            dt.count = size;
        }else{
            dt.code = dt.FLAG_FAIL;
            dt.msg = "查询失败";
        }
        closeout();
    }
    @RequestMapping(value="/getclassroomlist")//映射路径,拦截post请求
    public void getClassroomList(
            HttpServletRequest request,
            HttpServletResponse response,
            Model model) throws IOException{

        ClassRoomDAO crdao = DAOFactory.getClassRomeDAO();
        List list = crdao.getAllClasss();

        if (list!=null) {
            dt.code = dt.FLAG_SUCC;
            dt.msg = "查询成功，共查出" + list.size() + "条记录";
            dt.data = list;
        }else{
            dt.code = dt.FLAG_FAIL;
            dt.msg = "查询失败";
        }
        closeout();
    }
    @RequestMapping(value="/getroomlistBystate")//映射路径,拦截post请求
    public void getClassRoomListBystate(
            String roomName,
            String createtime,
            Integer state,
            Integer page,
            Integer limit,//一页多少条

            HttpServletRequest request,
            HttpServletResponse response,
            Model model) throws IOException {

        Expression exp = new Expression();
        if(roomName != null && !roomName.equals("")){
            exp.andEqu("roomName", roomName, String.class);}
        if (createtime !=null && !createtime.equals("")){
            exp.andEqu("createtime",createtime,String.class);
        }

        String wherecondition = exp.toString();
        ClassRoomDAO crdao = DAOFactory.getClassRomeDAO();
        List<TClassRoom> list = crdao.getTClassRoomByStatus(state,page, limit);
        int size = crdao.getClassAmount();
        if (list!=null) {
            dt.code = dt.FLAG_SUCC;
            dt.msg = "查询成功，共查出" + dt.count;
            dt.data = list;
            dt.count = size;
        }else{
            dt.code = dt.FLAG_FAIL;
            dt.msg = "查询失败";
        }
        closeout();
    }
    @RequestMapping(value="/addRoom")
    public void AddClassRoom(
            String className,
            String classtype,
            String description,
            HttpServletRequest request,
            HttpServletResponse response,
            Model model) throws IOException{

        TClassRoom room = new TClassRoom();
        room.setRoomName(className);
        room.setRoomtype(classtype);
        room.setCreatetime(Toolsutil.getdatetimes());
        room.setState(1);
        room.setDescription(description);

        ClassRoomDAO crdao = DAOFactory.getClassRomeDAO();
        if (crdao.addClass(room) > 0) {
            dt.code = dt.FLAG_SUCC;
            dt.msg = "添加成功";
        } else {
            dt.code = dt.FLAG_FAIL;
            dt.msg = "添加失败";
        }
        closeout();
    }

    /*
     * 实现一个角色的删除
     * @param userid 用户名
     * @throws IOException
     */
    @RequestMapping(value="/deleteroom")
    public void deleteClassRoom(
            String classroomid,
            HttpServletRequest request,
            HttpServletResponse response) throws IOException{
        ClassRoomDAO crdao = DAOFactory.getClassRomeDAO();
        if (classroomid !=null && !classroomid.equals("")){
            crdao.deleteClass(Integer.parseInt(classroomid));
            dt.code = dt.FLAG_SUCC;
            dt.msg = "删除成功";
        }else {
            dt.code = dt.FLAG_FAIL;
            dt.msg = "删除失败 ";
        }
        closeout();
    }

    @RequestMapping(value="/modifyroom")
    public void modifyClassRoom(
            Integer classroomid,
            String roomName,
            String roomtype,
            Integer state,
            String description,
            HttpServletRequest request,
            HttpServletResponse response,
            Model model) throws IOException{

        System.out.println(roomName);
        System.out.println(roomtype);
        TClassRoom room =new TClassRoom();
        room.setClassroomid(classroomid);
        room.setRoomName(roomName);
        room.setRoomtype(roomtype);
        room.setCreatetime(Toolsutil.getdatetimes());
        room.setState(state);
        room.setDescription(description);

        ClassRoomDAO crdao = DAOFactory.getClassRomeDAO();

        if (crdao.modifyClass(room)) {
            dt.code = dt.FLAG_SUCC;
            dt.msg = "修改成功";
        }else {
            dt.code = dt.FLAG_FAIL;
            dt.msg = "修改失败";
        }
        closeout();
    }
}
